package com.returnsoft.resource.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;

import com.returnsoft.resource.dto.AreaDto;
import com.returnsoft.resource.dto.CandidateDto;
import com.returnsoft.resource.dto.CandidateStateDto;
import com.returnsoft.resource.dto.CountryDto;
import com.returnsoft.resource.dto.DepartmentDto;
import com.returnsoft.resource.dto.DistrictDto;
import com.returnsoft.resource.dto.EducationLevelDto;
import com.returnsoft.resource.dto.InterviewDto;
import com.returnsoft.resource.dto.InterviewStateDto;
import com.returnsoft.resource.dto.OjtDto;
import com.returnsoft.resource.dto.OjtStateDto;
import com.returnsoft.resource.dto.ProvinceDto;
import com.returnsoft.resource.dto.RecruimentSourceDto;
import com.returnsoft.resource.dto.RecruiterDto;
import com.returnsoft.resource.dto.TrainerDto;
import com.returnsoft.resource.dto.TrainingDto;
import com.returnsoft.resource.dto.TrainingStateDto;
import com.returnsoft.resource.entity.Area;
import com.returnsoft.resource.entity.Candidate;
import com.returnsoft.resource.entity.CandidateState;
import com.returnsoft.resource.entity.Country;
import com.returnsoft.resource.entity.Department;
import com.returnsoft.resource.entity.District;
import com.returnsoft.resource.entity.EducationLevel;
import com.returnsoft.resource.entity.Interview;
import com.returnsoft.resource.entity.InterviewState;
import com.returnsoft.resource.entity.Ojt;
import com.returnsoft.resource.entity.OjtState;
import com.returnsoft.resource.entity.Province;
import com.returnsoft.resource.entity.RecruimentSource;
import com.returnsoft.resource.entity.Recruiter;
import com.returnsoft.resource.entity.Trainer;
import com.returnsoft.resource.entity.Training;
import com.returnsoft.resource.entity.TrainingState;
import com.returnsoft.resource.exception.ConversionExcepcion;

@Stateless
public class Conversion {

	public Trainer toTrainer(TrainerDto rd) throws ConversionExcepcion {
		try {
			Trainer r = new Trainer();
			r.setId(rd.getId());
			r.setUserId(rd.getUserId());
			if (rd.getAreasAvailable() != null
					&& rd.getAreasAvailable().size() > 0) {
				Set<Area> areas = new HashSet<Area>();
				for (AreaDto areaDto : rd.getAreasAvailable()) {
					Area area = new Area();
					area.setId(areaDto.getId());
					areas.add(area);
				}
				r.setAreas(areas);
			}
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}

	}

	public TrainerDto fromTrainer(Trainer r) throws ConversionExcepcion {
		try {
			TrainerDto rd = new TrainerDto();
			rd.setId(r.getId());
			rd.setUserId(r.getUserId());

			if (r.getAreas() != null && r.getAreas().size() > 0) {
				List<AreaDto> areasDto = new ArrayList<AreaDto>();
				for (Area area : r.getAreas()) {
					AreaDto areaDto = fromArea(area);
					areasDto.add(areaDto);
				}
				rd.setAreasAvailable(areasDto);
			}

			return rd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public Recruiter toRecruiter(RecruiterDto rd) throws ConversionExcepcion {
		try {
			Recruiter r = new Recruiter();
			r.setId(rd.getId());
			r.setUserId(rd.getUserId());
			if (rd.getAreasAvailable() != null
					&& rd.getAreasAvailable().size() > 0) {
				Set<Area> areas = new HashSet<Area>();
				for (AreaDto areaDto : rd.getAreasAvailable()) {
					Area area = new Area();
					area.setId(areaDto.getId());
					areas.add(area);
				}
				r.setAreas(areas);
			}
			return r;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public RecruiterDto fromRecruiter(Recruiter r) throws ConversionExcepcion {
		try {
			RecruiterDto rd = new RecruiterDto();
			rd.setId(r.getId());
			rd.setUserId(r.getUserId());

			if (r.getAreas() != null && r.getAreas().size() > 0) {
				List<AreaDto> areasDto = new ArrayList<AreaDto>();
				for (Area area : r.getAreas()) {
					AreaDto areaDto = fromArea(area);
					areasDto.add(areaDto);
				}
				rd.setAreasAvailable(areasDto);
			}

			return rd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public RecruimentSourceDto fromRecruimentSource(RecruimentSource rs)
			throws ConversionExcepcion {
		try {
			RecruimentSourceDto rsd = new RecruimentSourceDto();
			rsd.setId(rs.getId());
			rsd.setName(rs.getName());
			return rsd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public AreaDto fromArea(Area d) throws ConversionExcepcion {
		try {
			AreaDto dd = new AreaDto();
			dd.setId(d.getId());
			dd.setName(d.getName());
			if (d.getArea() != null) {
				AreaDto areaDto = fromArea(d.getArea());
				dd.setArea(areaDto);
			}
			return dd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public CountryDto fromCountry(Country c) throws ConversionExcepcion {
		try {
			CountryDto cd = new CountryDto();
			cd.setId(c.getId());
			cd.setName(c.getName());
			return cd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public DepartmentDto fromDepartment(Department d)
			throws ConversionExcepcion {
		try {
			DepartmentDto dd = new DepartmentDto();
			dd.setId(d.getId());
			dd.setName(d.getName());
			if (d.getCountry() != null) {
				CountryDto pd = fromCountry(d.getCountry());
				dd.setCountry(pd);
			}
			return dd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public ProvinceDto fromProvince(Province p) throws ConversionExcepcion {
		try {
			ProvinceDto pd = new ProvinceDto();
			pd.setId(p.getId());
			pd.setName(p.getName());
			if (p.getDepartment() != null) {
				DepartmentDto cd = fromDepartment(p.getDepartment());
				pd.setDepartment(cd);
			}
			return pd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public DistrictDto fromDistrict(District d) throws ConversionExcepcion {
		try {
			DistrictDto dd = new DistrictDto();
			dd.setId(d.getId());
			dd.setName(d.getName());
			if (d.getProvince() != null) {
				ProvinceDto pd = fromProvince(d.getProvince());
				dd.setProvince(pd);
			}
			return dd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}
	
	public CandidateStateDto fromCandidateState(CandidateState is)
			throws ConversionExcepcion {
		try {
			CandidateStateDto isd = new CandidateStateDto();
			isd.setId(is.getId());
			isd.setName(is.getName());
			isd.setIsPending(is.getIsPending());
			isd.setIsGoal(is.getIsGoal());
			return isd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}
	
	public EducationLevelDto fromEducationLevel(EducationLevel el)
			throws ConversionExcepcion {
		try {
			EducationLevelDto eld = new EducationLevelDto();
			eld.setId(el.getId());
			eld.setName(el.getName());
			return eld;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}
	

	public InterviewStateDto fromInterviewState(InterviewState is)
			throws ConversionExcepcion {
		try {
			InterviewStateDto isd = new InterviewStateDto();
			isd.setId(is.getId());
			isd.setName(is.getName());
			isd.setIsGoal(is.getIsGoal());
			isd.setIsPending(is.getIsPending());
			return isd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}
	
	

	public TrainingStateDto fromTrainingState(TrainingState is)
			throws ConversionExcepcion {
		try {
			TrainingStateDto isd = new TrainingStateDto();
			isd.setId(is.getId());
			isd.setName(is.getName());
			isd.setIsPending(is.getIsPending());
			isd.setIsGoal(is.getIsGoal());
			return isd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public OjtStateDto fromOjtState(OjtState is) throws ConversionExcepcion {
		try {
			OjtStateDto isd = new OjtStateDto();
			isd.setId(is.getId());
			isd.setName(is.getName());
			isd.setIsPending(is.getIsPending());
			isd.setIsGoal(is.getIsGoal());
			return isd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public CandidateDto fromCandidate(Candidate p) throws ConversionExcepcion {
		try {
			CandidateDto pd = new CandidateDto();
			pd.setId(p.getId());
			pd.setBornDate(p.getBornDate());
			pd.setCellPhone(p.getCellPhone());
			pd.setDocumentType(p.getDocumentType());
			pd.setFirstname(p.getFirstname());
			pd.setLastname(p.getLastname());
			pd.setDocumentNumber(p.getDocumentNumber());
			pd.setEntryType(p.getEntryType());
			pd.setExperience(p.getExperience());
			pd.setMail(p.getMail());
			pd.setPhone(p.getPhone());
			pd.setCreatedAt(p.getCreatedAt());
			pd.setHomeAddress(p.getHomeAddress());
			pd.setLastJob(p.getLastJob());
			if (p.getArea() != null) {
				AreaDto area = fromArea(p.getArea());
				pd.setArea(area);
			}
			if (p.getCountry() != null) {
				CountryDto country = fromCountry(p.getCountry());
				pd.setCountry(country);
			}
			if (p.getDepartment() != null) {
				DepartmentDto department = fromDepartment(p.getDepartment());
				pd.setDepartment(department);
			}
			if (p.getProvince() != null) {
				ProvinceDto province = fromProvince(p.getProvince());
				pd.setProvince(province);
			}
			if (p.getDistrict() != null) {
				DistrictDto district = fromDistrict(p.getDistrict());
				pd.setDistrict(district);
			}
			if (p.getRecruimentSource() != null) {
				RecruimentSourceDto recruimentSource = fromRecruimentSource(p
						.getRecruimentSource());
				pd.setRecruimentSource(recruimentSource);
			}
			if (p.getRecruiter() != null) {
				RecruiterDto recruiter = fromRecruiter(p
						.getRecruiter());
				pd.setRecruiter(recruiter);
			}
			if(p.getEducationLevel() != null){
				EducationLevelDto educationLevel = new EducationLevelDto();
				educationLevel.setId(p.getEducationLevel().getId());
				pd.setEducationLevel(educationLevel);
			}

			return pd;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	// ///
	public Candidate toCandidate(CandidateDto pd) throws ConversionExcepcion {
		try {
			Candidate p = new Candidate();
			p.setId(pd.getId());
			p.setBornDate(pd.getBornDate());
			p.setCellPhone(pd.getCellPhone());
			p.setDocumentType(pd.getDocumentType());
			p.setFirstname(pd.getFirstname());
			p.setLastname(pd.getLastname());
			p.setDocumentNumber(pd.getDocumentNumber());
			p.setEntryType(pd.getEntryType());
			p.setExperience(pd.getExperience());
			p.setMail(pd.getMail());
			p.setPhone(pd.getPhone());
			p.setCreatedAt(pd.getCreatedAt());
			p.setScheduledAt(pd.getScheduledAt());
			p.setHomeAddress(pd.getHomeAddress());
			p.setLastJob(pd.getLastJob());
			if (pd.getCountry() != null) {
				Country country = new Country();
				country.setId(pd.getCountry().getId());
				p.setCountry(country);
			}
			if (pd.getDepartment() != null) {
				Department department = new Department();
				department.setId(pd.getDepartment().getId());
				p.setDepartment(department);
			}
			if (pd.getProvince() != null) {
				Province province = new Province();
				province.setId(pd.getProvince().getId());
				p.setProvince(province);
			}
			if (pd.getDistrict() != null) {
				District district = new District();
				district.setId(pd.getDistrict().getId());
				p.setDistrict(district);
			}
			if (pd.getRecruimentSource() != null) {
				RecruimentSource recruimentSource = new RecruimentSource();
				recruimentSource.setId(pd.getRecruimentSource().getId());
				p.setRecruimentSource(recruimentSource);
			}
			if (pd.getArea() != null) {
				Area area = new Area();
				area.setId(pd.getArea().getId());
				p.setArea(area);
			}
			if (pd.getCandidateState() != null) {
				CandidateState state = new CandidateState();
				state.setId(pd.getCandidateState().getId());
				p.setCandidateState(state);
			}
			if (pd.getRecruiter() != null) {
				Recruiter recruiter = new Recruiter();
				recruiter.setId(pd.getRecruiter().getId());
				p.setRecruiter(recruiter);
			}
			if (pd.getEducationLevel() != null) {
				EducationLevel educationLevel = new EducationLevel();
				educationLevel.setId(pd.getEducationLevel().getId());
				p.setEducationLevel(educationLevel);
			}

			return p;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public InterviewDto fromInterview(Interview i) throws ConversionExcepcion {
		try {
			InterviewDto id = new InterviewDto();
			id.setId(i.getId());
			id.setComment(i.getComment());
			id.setCreatedAt(i.getCreatedAt());
			id.setInterviewedAt(i.getInterviewedAt());
			if (i.getRecruiter() != null) {
				RecruiterDto recruiter = new RecruiterDto();
				recruiter.setId(i.getRecruiter().getId());

				id.setRecruiter(recruiter);
			}
			if (i.getCandidate() != null) {
				CandidateDto candidate = new CandidateDto();
				candidate.setId(i.getCandidate().getId());
				candidate.setDocumentNumber(i.getCandidate()
						.getDocumentNumber());
				candidate.setFirstname(i.getCandidate().getFirstname());
				candidate.setLastname(i.getCandidate().getLastname());
				id.setCandidate(candidate);
			}

			if (i.getInterviewState() != null) {
				InterviewStateDto state = new InterviewStateDto();
				state.setId(i.getInterviewState().getId());
				state.setName(i.getInterviewState().getName());
				state.setIsGoal(i.getInterviewState().getIsGoal());
				state.setIsPending(i.getInterviewState().getIsPending());
				id.setInterviewState(state);
			}
			return id;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public Interview toInterview(InterviewDto dto) throws ConversionExcepcion {

		try {

			Interview entity = new Interview();
			entity.setId(dto.getId());
			entity.setComment(dto.getComment());
			entity.setCreatedAt(dto.getCreatedAt());
			entity.setInterviewedAt(dto.getInterviewedAt());
			if (dto.getRecruiter() != null) {
				Recruiter recruiter = new Recruiter();
				recruiter.setId(dto.getRecruiter().getId());
				entity.setRecruiter(recruiter);
			}
			if (dto.getCandidate() != null) {
				Candidate candidate = new Candidate();
				candidate.setId(dto.getCandidate().getId());
				entity.setCandidate(candidate);
			}
			if (dto.getInterviewState() != null) {
				InterviewState state = new InterviewState();
				state.setId(dto.getInterviewState().getId());
				entity.setInterviewState(state);
			}
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public TrainingDto fromTraining(Training i) throws ConversionExcepcion {
		try {
			TrainingDto id = new TrainingDto();
			id.setId(i.getId());
			id.setComment(i.getComment());
			id.setCreatedAt(i.getCreatedAt());
			id.setTrainingAt(i.getTrainingAt());
			if (i.getTrainer() != null) {
				TrainerDto trainer = new TrainerDto();
				trainer.setId(i.getTrainer().getId());

				id.setTrainer(trainer);
			}
			if (i.getInterview() != null) {
				InterviewDto interview = new InterviewDto();
				interview.setId(i.getInterview().getId());
				if (i.getInterview().getCandidate() != null) {
					CandidateDto candidate = new CandidateDto();
					candidate.setId(i.getInterview().getCandidate().getId());
					candidate.setFirstname(i.getInterview().getCandidate()
							.getFirstname());
					candidate.setLastname(i.getInterview().getCandidate()
							.getLastname());
					candidate.setDocumentNumber(i.getInterview().getCandidate()
							.getDocumentNumber());
					interview.setCandidate(candidate);
				}
				id.setInterview(interview);
			}

			if (i.getTrainingState() != null) {
				TrainingStateDto state = new TrainingStateDto();
				state.setId(i.getTrainingState().getId());
				state.setName(i.getTrainingState().getName());
				state.setIsGoal(i.getTrainingState().getIsGoal());
				state.setIsPending(i.getTrainingState().getIsPending());
				id.setTrainingState(state);
			}
			if (i.getRecruiter() != null) {
				RecruiterDto recruiter = new RecruiterDto();
				recruiter.setId(i.getRecruiter().getId());
				id.setRecruiter(recruiter);
			}
			return id;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public Training toTraining(TrainingDto dto) throws ConversionExcepcion {
		try {

			Training entity = new Training();
			entity.setId(dto.getId());
			entity.setComment(dto.getComment());
			entity.setCreatedAt(dto.getCreatedAt());
			entity.setTrainingAt(dto.getTrainingAt());
			if (dto.getTrainer() != null) {
				Trainer trainer = new Trainer();
				trainer.setId(dto.getTrainer().getId());
				entity.setTrainer(trainer);
			}
			if (dto.getInterview() != null) {
				Interview interview = new Interview();
				interview.setId(dto.getInterview().getId());
				entity.setInterview(interview);
			}
			if (dto.getTrainingState() != null) {
				TrainingState state = new TrainingState();
				state.setId(dto.getTrainingState().getId());
				entity.setTrainingState(state);
			}
			if (dto.getRecruiter() != null) {
				Recruiter recruiter = new Recruiter();
				recruiter.setId(dto.getRecruiter().getId());
				entity.setRecruiter(recruiter);
			}
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public OjtDto fromOjt(Ojt i) throws ConversionExcepcion {
		try {
			OjtDto id = new OjtDto();
			id.setId(i.getId());
			id.setComment(i.getComment());
			id.setCreatedAt(i.getCreatedAt());
			id.setOjtAt(i.getOjtAt());

			if (i.getRecruiter() != null) {
				RecruiterDto recruiter = new RecruiterDto();
				recruiter.setId(i.getRecruiter().getId());

				id.setRecruiter(recruiter);
			}
			if (i.getTraining() != null) {
				TrainingDto training = new TrainingDto();
				training.setId(i.getTraining().getId());
				if (i.getTraining().getInterview() != null) {
					InterviewDto interview = new InterviewDto();
					interview.setId(i.getTraining().getInterview().getId());
					if (i.getTraining().getInterview().getCandidate() != null) {
						CandidateDto candidate = new CandidateDto();
						candidate.setId(i.getTraining().getInterview()
								.getCandidate().getId());
						candidate.setFirstname(i.getTraining().getInterview()
								.getCandidate().getFirstname());
						candidate.setLastname(i.getTraining().getInterview()
								.getCandidate().getLastname());
						candidate.setDocumentNumber(i.getTraining()
								.getInterview().getCandidate()
								.getDocumentNumber());
						interview.setCandidate(candidate);
					}
					training.setInterview(interview);
				}
				id.setTraining(training);
			}

			if (i.getOjtState() != null) {
				OjtStateDto state = new OjtStateDto();
				state.setId(i.getOjtState().getId());
				state.setName(i.getOjtState().getName());
				state.setIsGoal(i.getOjtState().getIsGoal());
				state.setIsPending(i.getOjtState().getIsPending());
				id.setOjtState(state);
			}
			return id;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

	public Ojt toOjt(OjtDto dto) throws ConversionExcepcion {
		try {

			Ojt entity = new Ojt();
			entity.setId(dto.getId());
			entity.setComment(dto.getComment());
			entity.setCreatedAt(dto.getCreatedAt());
			entity.setOjtAt(dto.getOjtAt());
			if (dto.getRecruiter() != null) {
				Recruiter recruiter = new Recruiter();
				recruiter.setId(dto.getRecruiter().getId());
				entity.setRecruiter(recruiter);
			}
			if (dto.getTraining() != null) {
				Training training = new Training();
				training.setId(dto.getTraining().getId());
				entity.setTraining(training);
			}
			if (dto.getOjtState() != null) {
				OjtState state = new OjtState();
				state.setId(dto.getOjtState().getId());
				entity.setOjtState(state);
			}
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConversionExcepcion(e.getMessage());
		}
	}

}
